<template>
	<view>
		<uni-search-bar @confirm="" @input="onInput" :radius="18" clearButton='auto'/>
    
    <!-- 搜索建议列表 -->
    <view class="sugg-list" v-if="keyWord.length !== 0">
      <view class="sugg-item" v-for="(item,i) in suggList" @click="gotoGoods_detail(item)" :key='i'>
        <text>{{item.goods_name}}</text>
        <uni-icons type="forward" size="16"></uni-icons>
      </view>
    </view>
    
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="16" @click='clearHistory'></uni-icons>
      </view>
      <view class="history-item">
        <uni-tag :text="item" v-for="(item,i) in historyList" :key='i' @click="gotoGoodsList(item)"></uni-tag>
      </view>
    </view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				keyWord:'',
        timer: null,
        suggList:[],
        historyList: JSON.parse(uni.getStorageSync("searchHistory") || '[]')
			};
		},
    methods:{
      onInput(e) {
        // 每次触发input事件，清空上次的计时器，重新开启计时器
        clearTimeout(this.timer)
        this.timer = setTimeout(()=>{
          this.keyWord = e.value
          this.getSuggList()
          // 将关键字存储到搜索历史
          this.saveHistory()
        },500)
      },
      async getSuggList() {
        const {data: res} = await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.keyWord })
        if(res.meta.status !== 200) return uni.$message()
        this.suggList = res.message
      },
      gotoGoods_detail(item) {
        // console.log(item.goods_id)
        uni.navigateTo({
          // url:'/subpkg/goods_detail/goods_detail' + item.goods_id
        })
      },
      // 存储搜索历史
      saveHistory() {
        if(this.keyWord.trim().length === 0) return
        let index = this.historyList.findIndex(item=>{
          return item === this.keyWord
        })
        if(index !== -1) {
          this.historyList.splice(index,1)
        }
        this.historyList.unshift(this.keyWord)
        uni.setStorageSync('searchHistory',JSON.stringify(this.historyList))
      },
      clearHistory() {
        this.historyList = []
        uni.setStorageSync('searchHistory','')
      },
      // 点击跳转到商品列表页面
      gotoGoodsList(kw) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?query=' + kw
        })
      }
    }
	}
</script>

<style lang="scss">
.sugg-list {
  padding: 0 5px;
  .sugg-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #efefef;
    text {
      font-size: 14px;
      line-height: 45px;
      // 一行显示
      white-space: nowrap;
      overflow: hidden;
      // 超出省略
      text-overflow: ellipsis;
    }
  }
}

// 搜索历史
.history-box {
  padding: 0 5px;
  .history-title {
    padding: 0 5px;
    display: flex;
    justify-content: space-between;
    line-height: 40px;
    border-bottom: 1px solid #efefef;
    font-size: 16px;
  }
  .history-item {
    display: flex;
    .uni-tag {
      margin: 5px 5px 0 0;
    }
  }
}
</style>
